{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "提示：当前环境pandas版本为0.25，get_price与get_fundamentals_continuously接口panel参数将固定为False\n",
      "注意：0.25以上版本pandas不支持panel，如使用该数据结构和相关函数请注意修改\n"
     ]
    }
   ],
   "source": [
    "# 导入函数库\n",
    "from jqdatasdk import *\n",
    "import pandas as pd \n",
    "import seaborn as sns\n",
    "#显示行数\n",
    "pd.set_option('display.max_rows',20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "auth success \n"
     ]
    }
   ],
   "source": [
    "# auth('ID','Password')\n",
    "\n",
    "auth('138***','****') #修改为自己的用户名密码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'total': 1000000, 'spare': 983374}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_query_count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>display_name</th>\n",
       "      <th>name</th>\n",
       "      <th>start_date</th>\n",
       "      <th>end_date</th>\n",
       "      <th>type</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>000001.XSHE</th>\n",
       "      <td>平安银行</td>\n",
       "      <td>PAYX</td>\n",
       "      <td>1991-04-03</td>\n",
       "      <td>2200-01-01</td>\n",
       "      <td>stock</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000002.XSHE</th>\n",
       "      <td>万科A</td>\n",
       "      <td>WKA</td>\n",
       "      <td>1991-01-29</td>\n",
       "      <td>2200-01-01</td>\n",
       "      <td>stock</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000004.XSHE</th>\n",
       "      <td>国农科技</td>\n",
       "      <td>GNKJ</td>\n",
       "      <td>1990-12-01</td>\n",
       "      <td>2200-01-01</td>\n",
       "      <td>stock</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000005.XSHE</th>\n",
       "      <td>世纪星源</td>\n",
       "      <td>SJXY</td>\n",
       "      <td>1990-12-10</td>\n",
       "      <td>2200-01-01</td>\n",
       "      <td>stock</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000006.XSHE</th>\n",
       "      <td>深振业A</td>\n",
       "      <td>SZYA</td>\n",
       "      <td>1992-04-27</td>\n",
       "      <td>2200-01-01</td>\n",
       "      <td>stock</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>688596.XSHG</th>\n",
       "      <td>正帆科技</td>\n",
       "      <td>ZFKJ</td>\n",
       "      <td>2020-08-20</td>\n",
       "      <td>2200-01-01</td>\n",
       "      <td>stock</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>688598.XSHG</th>\n",
       "      <td>金博股份</td>\n",
       "      <td>JBGF</td>\n",
       "      <td>2020-05-18</td>\n",
       "      <td>2200-01-01</td>\n",
       "      <td>stock</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>688599.XSHG</th>\n",
       "      <td>天合光能</td>\n",
       "      <td>THGN</td>\n",
       "      <td>2020-06-10</td>\n",
       "      <td>2200-01-01</td>\n",
       "      <td>stock</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>688600.XSHG</th>\n",
       "      <td>皖仪科技</td>\n",
       "      <td>WYKJ</td>\n",
       "      <td>2020-07-03</td>\n",
       "      <td>2200-01-01</td>\n",
       "      <td>stock</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>688981.XSHG</th>\n",
       "      <td>中芯国际</td>\n",
       "      <td>ZXGJ</td>\n",
       "      <td>2020-07-16</td>\n",
       "      <td>2200-01-01</td>\n",
       "      <td>stock</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4143 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            display_name  name start_date   end_date   type\n",
       "000001.XSHE         平安银行  PAYX 1991-04-03 2200-01-01  stock\n",
       "000002.XSHE          万科A   WKA 1991-01-29 2200-01-01  stock\n",
       "000004.XSHE         国农科技  GNKJ 1990-12-01 2200-01-01  stock\n",
       "000005.XSHE         世纪星源  SJXY 1990-12-10 2200-01-01  stock\n",
       "000006.XSHE         深振业A  SZYA 1992-04-27 2200-01-01  stock\n",
       "...                  ...   ...        ...        ...    ...\n",
       "688596.XSHG         正帆科技  ZFKJ 2020-08-20 2200-01-01  stock\n",
       "688598.XSHG         金博股份  JBGF 2020-05-18 2200-01-01  stock\n",
       "688599.XSHG         天合光能  THGN 2020-06-10 2200-01-01  stock\n",
       "688600.XSHG         皖仪科技  WYKJ 2020-07-03 2200-01-01  stock\n",
       "688981.XSHG         中芯国际  ZXGJ 2020-07-16 2200-01-01  stock\n",
       "\n",
       "[4143 rows x 5 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_all_securities()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 价格数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`get_price(security, start_date=None, end_date=None, frequency='daily', fields=None, skip_paused=False, fq='pre', count=None, panel=True, fill_paused=True)`\n",
    "\n",
    "**参数**\n",
    "\n",
    "- security: 一支股票代码或者一个股票代码的list\n",
    "- count: **与 start_date 二选一，不可同时使用**. 数量, 返回的结果集的行数, 即表示获取 end_date 之前几个 frequency 的数据\n",
    "- start_date: **与 count 二选一，不可同时使用**. 字符串或者 [datetime.datetime]/[datetime.date] 对象, 开始时间.\n",
    "  - 如果 count 和 start_date 参数都没有, 则 start_date 生效, 值是 '2015-01-01'. 注意:\n",
    "  - 当取分钟数据时, 时间可以精确到分钟, 比如: 传入 `datetime.datetime(2015, 1, 1, 10, 0, 0)` 或者 `'2015-01-01 10:00:00'`.\n",
    "  - 当取分钟数据时, 如果只传入日期, 则日内时间是当日的 00:00:00.\n",
    "  - 当取天数据时, 传入的日内时间会被忽略\n",
    "- end_date: 格式同上, 结束时间, 默认是'2015-12-31', 包含此日期. **注意: 当取分钟数据时, 如果 end_date 只有日期, 则日内时间等同于 00:00:00, 所以返回的数据是不包括 end_date 这一天的**.\n",
    "- frequency: 单位时间长度, 几天或者几分钟, 现在支持'Xd','Xm', 'daily'(等同于'1d'), 'minute'(等同于'1m'), X是一个正整数, 分别表示X天和X分钟(不论是按天还是按分钟回测都能拿到这两种单位的数据), 注意, 当X > 1时, fields只支持['open', 'close', 'high', 'low', 'volume', 'money']这几个标准字段. 默认值是daily\n",
    "- fields: 字符串list, 选择要获取的行情数据字段, 默认是None(表示['open', 'close', 'high', 'low', 'volume', 'money']这几个标准字段), 支持[SecurityUnitData](https://www.joinquant.com/help/api/help?name=api#SecurityUnitData)里面的所有基本属性,，包含：['open', 'close', 'low', 'high', 'volume', 'money', 'factor', 'high_limit','low_limit', 'avg', 'pre_close', 'paused', 'open_interest'], open_interest为**期货持仓量**\n",
    "- skip_paused: 是否跳过不交易日期(包括停牌, 未上市或者退市后的日期). 如果不跳过, 停牌时会使用停牌前的数据填充(具体请看SecurityUnitData的paused属性), 上市前或者退市后数据都为 nan, 但要注意:\n",
    "  - 默认为 False\n",
    "  - 当 skip_paused 是 True 时, 获取多个标的时需要将panel参数设置为False(panel结构需要索引对齐)\n",
    "- fq: 复权选项:\n",
    "  - `'pre'`: 前复权\n",
    "  - `None`: 不复权, 返回实际价格\n",
    "  - `'post'`: 后复权\n",
    "- panel：当传入股票列表时，指定返回结果是否使用panel格式，默认为True；指定panel=False时返回dataframe格式。\n",
    "- fill_paused：对于停牌股票的价格处理，默认为True；True表示用pre_close价格填充；False 表示使用NAN填充停牌的股票价格。\n",
    "\n",
    "**返回**\n",
    "\n",
    "- **请注意, 为了方便比较一只股票的多个属性, 同时也满足对比多只股票的一个属性的需求, 我们在security参数是一只股票和多只股票时返回的结构完全不一样 (默认panel=True时)**\n",
    "\n",
    "- 如果是一支股票, 则返回[pandas.DataFrame]对象, 行索引是[datetime.datetime]对象, 列索引是行情字段名字, 比如'open'/'close'. 比如: `get_price('000300.XSHG')[:2]` 返回:\n",
    "\n",
    "  | ---                 | open    | close   | high    | low     | volume      | money          |\n",
    "  | :------------------ | :------ | :------ | :------ | :------ | :---------- | :------------- |\n",
    "  | 2015-01-05 00:00:00 | 3566.09 | 3641.54 | 3669.04 | 3551.51 | 451198098.0 | 519849817448.0 |\n",
    "  | 2015-01-06 00:00:00 | 3608.43 | 3641.06 | 3683.23 | 3587.23 | 420962185.0 | 498529588258.0 |\n",
    "\n",
    "- 如果是多支股票, 则返回[pandas.Panel]对象, 里面是很多[pandas.DataFrame]对象, 索引是行情字段(open/close/…), 每个[pandas.DataFrame]的行索引是[datetime.datetime]对象, 列索引是股票代号. 比如`get_price(['000300.XSHG', '000001.XSHE'])['open'][:2]`返回:\n",
    "\n",
    "  | ---                 | 000300.XSHG | 000001.XSHE |\n",
    "  | :------------------ | :---------- | :---------- |\n",
    "  | 2015-01-05 00:00:00 | 3566.09     | 13.21       |\n",
    "  | 2015-01-06 00:00:00 | 3608.43     | 13.09       |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>money</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-01-05</th>\n",
       "      <td>10.24</td>\n",
       "      <td>10.26</td>\n",
       "      <td>10.43</td>\n",
       "      <td>9.99</td>\n",
       "      <td>446489383.0</td>\n",
       "      <td>4.565388e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-06</th>\n",
       "      <td>10.15</td>\n",
       "      <td>10.11</td>\n",
       "      <td>10.50</td>\n",
       "      <td>9.96</td>\n",
       "      <td>338159640.0</td>\n",
       "      <td>3.453446e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-07</th>\n",
       "      <td>9.97</td>\n",
       "      <td>9.92</td>\n",
       "      <td>10.14</td>\n",
       "      <td>9.80</td>\n",
       "      <td>265374121.0</td>\n",
       "      <td>2.634796e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-08</th>\n",
       "      <td>9.93</td>\n",
       "      <td>9.58</td>\n",
       "      <td>9.97</td>\n",
       "      <td>9.55</td>\n",
       "      <td>219732012.0</td>\n",
       "      <td>2.128003e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-09</th>\n",
       "      <td>9.55</td>\n",
       "      <td>9.66</td>\n",
       "      <td>10.17</td>\n",
       "      <td>9.42</td>\n",
       "      <td>391555169.0</td>\n",
       "      <td>3.835378e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-25</th>\n",
       "      <td>9.60</td>\n",
       "      <td>9.62</td>\n",
       "      <td>9.67</td>\n",
       "      <td>9.56</td>\n",
       "      <td>51558377.0</td>\n",
       "      <td>4.956380e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-28</th>\n",
       "      <td>9.64</td>\n",
       "      <td>9.29</td>\n",
       "      <td>9.66</td>\n",
       "      <td>9.29</td>\n",
       "      <td>106046218.0</td>\n",
       "      <td>1.003010e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-29</th>\n",
       "      <td>9.30</td>\n",
       "      <td>9.38</td>\n",
       "      <td>9.38</td>\n",
       "      <td>9.28</td>\n",
       "      <td>79920944.0</td>\n",
       "      <td>7.447184e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-30</th>\n",
       "      <td>9.38</td>\n",
       "      <td>9.38</td>\n",
       "      <td>9.39</td>\n",
       "      <td>9.27</td>\n",
       "      <td>68685228.0</td>\n",
       "      <td>6.412772e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-31</th>\n",
       "      <td>9.38</td>\n",
       "      <td>9.30</td>\n",
       "      <td>9.41</td>\n",
       "      <td>9.29</td>\n",
       "      <td>63345821.0</td>\n",
       "      <td>5.916436e+08</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>244 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             open  close   high   low       volume         money\n",
       "2015-01-05  10.24  10.26  10.43  9.99  446489383.0  4.565388e+09\n",
       "2015-01-06  10.15  10.11  10.50  9.96  338159640.0  3.453446e+09\n",
       "2015-01-07   9.97   9.92  10.14  9.80  265374121.0  2.634796e+09\n",
       "2015-01-08   9.93   9.58   9.97  9.55  219732012.0  2.128003e+09\n",
       "2015-01-09   9.55   9.66  10.17  9.42  391555169.0  3.835378e+09\n",
       "...           ...    ...    ...   ...          ...           ...\n",
       "2015-12-25   9.60   9.62   9.67  9.56   51558377.0  4.956380e+08\n",
       "2015-12-28   9.64   9.29   9.66  9.29  106046218.0  1.003010e+09\n",
       "2015-12-29   9.30   9.38   9.38  9.28   79920944.0  7.447184e+08\n",
       "2015-12-30   9.38   9.38   9.39  9.27   68685228.0  6.412772e+08\n",
       "2015-12-31   9.38   9.30   9.41  9.29   63345821.0  5.916436e+08\n",
       "\n",
       "[244 rows x 6 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = get_price('000001.XSHE')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-01-02</th>\n",
       "      <td>16.37</td>\n",
       "      <td>16.58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-03</th>\n",
       "      <td>16.65</td>\n",
       "      <td>16.89</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-06</th>\n",
       "      <td>16.72</td>\n",
       "      <td>16.78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-07</th>\n",
       "      <td>16.84</td>\n",
       "      <td>16.86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-08</th>\n",
       "      <td>16.71</td>\n",
       "      <td>16.38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-09</th>\n",
       "      <td>16.53</td>\n",
       "      <td>16.51</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-10</th>\n",
       "      <td>16.51</td>\n",
       "      <td>16.41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-13</th>\n",
       "      <td>16.47</td>\n",
       "      <td>16.70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-14</th>\n",
       "      <td>16.70</td>\n",
       "      <td>16.48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-15</th>\n",
       "      <td>16.51</td>\n",
       "      <td>16.24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-16</th>\n",
       "      <td>16.24</td>\n",
       "      <td>16.05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-17</th>\n",
       "      <td>16.10</td>\n",
       "      <td>16.11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-20</th>\n",
       "      <td>16.15</td>\n",
       "      <td>16.17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-21</th>\n",
       "      <td>16.06</td>\n",
       "      <td>15.73</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-22</th>\n",
       "      <td>15.65</td>\n",
       "      <td>15.82</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-23</th>\n",
       "      <td>15.65</td>\n",
       "      <td>15.28</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             open  close\n",
       "2020-01-02  16.37  16.58\n",
       "2020-01-03  16.65  16.89\n",
       "2020-01-06  16.72  16.78\n",
       "2020-01-07  16.84  16.86\n",
       "2020-01-08  16.71  16.38\n",
       "2020-01-09  16.53  16.51\n",
       "2020-01-10  16.51  16.41\n",
       "2020-01-13  16.47  16.70\n",
       "2020-01-14  16.70  16.48\n",
       "2020-01-15  16.51  16.24\n",
       "2020-01-16  16.24  16.05\n",
       "2020-01-17  16.10  16.11\n",
       "2020-01-20  16.15  16.17\n",
       "2020-01-21  16.06  15.73\n",
       "2020-01-22  15.65  15.82\n",
       "2020-01-23  15.65  15.28"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = get_price('000001.XSHE', start_date='2020-01-01',             \\\n",
    "            end_date='2020-01-31 23:00:00',                         \\\n",
    "               frequency='daily', fields=['open', 'close'])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-01-29</th>\n",
       "      <td>8.85</td>\n",
       "      <td>8.91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-30</th>\n",
       "      <td>8.92</td>\n",
       "      <td>8.92</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            open  close\n",
       "2015-01-29  8.85   8.91\n",
       "2015-01-30  8.92   8.92"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取获得000001.XSHG在2015年01月31日前2个交易日的数据\n",
    "df = get_price('000001.XSHE', count = 2, end_date='2015-01-31', frequency='daily', fields=['open', 'close']) \n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>time</th>\n",
       "      <th>code</th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>money</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2015-01-05</td>\n",
       "      <td>601857.XSHG</td>\n",
       "      <td>9.70</td>\n",
       "      <td>10.58</td>\n",
       "      <td>10.58</td>\n",
       "      <td>9.58</td>\n",
       "      <td>419005913.0</td>\n",
       "      <td>4.228175e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2015-01-06</td>\n",
       "      <td>601857.XSHG</td>\n",
       "      <td>10.76</td>\n",
       "      <td>10.40</td>\n",
       "      <td>10.97</td>\n",
       "      <td>10.23</td>\n",
       "      <td>437608197.0</td>\n",
       "      <td>4.626943e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2015-01-07</td>\n",
       "      <td>601857.XSHG</td>\n",
       "      <td>10.23</td>\n",
       "      <td>11.02</td>\n",
       "      <td>11.05</td>\n",
       "      <td>10.17</td>\n",
       "      <td>370177860.0</td>\n",
       "      <td>3.900464e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2015-01-08</td>\n",
       "      <td>601857.XSHG</td>\n",
       "      <td>10.85</td>\n",
       "      <td>10.86</td>\n",
       "      <td>11.69</td>\n",
       "      <td>10.74</td>\n",
       "      <td>478644195.0</td>\n",
       "      <td>5.362077e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2015-01-09</td>\n",
       "      <td>601857.XSHG</td>\n",
       "      <td>10.74</td>\n",
       "      <td>10.74</td>\n",
       "      <td>11.30</td>\n",
       "      <td>10.53</td>\n",
       "      <td>334132540.0</td>\n",
       "      <td>3.640000e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24395</th>\n",
       "      <td>2015-12-25</td>\n",
       "      <td>601186.XSHG</td>\n",
       "      <td>12.54</td>\n",
       "      <td>12.64</td>\n",
       "      <td>12.67</td>\n",
       "      <td>12.41</td>\n",
       "      <td>20541178.0</td>\n",
       "      <td>2.579197e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24396</th>\n",
       "      <td>2015-12-28</td>\n",
       "      <td>601186.XSHG</td>\n",
       "      <td>12.71</td>\n",
       "      <td>12.23</td>\n",
       "      <td>12.71</td>\n",
       "      <td>12.20</td>\n",
       "      <td>36161860.0</td>\n",
       "      <td>4.518754e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24397</th>\n",
       "      <td>2015-12-29</td>\n",
       "      <td>601186.XSHG</td>\n",
       "      <td>12.25</td>\n",
       "      <td>12.28</td>\n",
       "      <td>12.31</td>\n",
       "      <td>12.18</td>\n",
       "      <td>20947996.0</td>\n",
       "      <td>2.561569e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24398</th>\n",
       "      <td>2015-12-30</td>\n",
       "      <td>601186.XSHG</td>\n",
       "      <td>12.36</td>\n",
       "      <td>12.34</td>\n",
       "      <td>12.50</td>\n",
       "      <td>12.27</td>\n",
       "      <td>21991434.0</td>\n",
       "      <td>2.717333e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24399</th>\n",
       "      <td>2015-12-31</td>\n",
       "      <td>601186.XSHG</td>\n",
       "      <td>12.34</td>\n",
       "      <td>12.27</td>\n",
       "      <td>12.53</td>\n",
       "      <td>12.25</td>\n",
       "      <td>28287535.0</td>\n",
       "      <td>3.501418e+08</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>24400 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            time         code   open  close   high    low       volume  \\\n",
       "0     2015-01-05  601857.XSHG   9.70  10.58  10.58   9.58  419005913.0   \n",
       "1     2015-01-06  601857.XSHG  10.76  10.40  10.97  10.23  437608197.0   \n",
       "2     2015-01-07  601857.XSHG  10.23  11.02  11.05  10.17  370177860.0   \n",
       "3     2015-01-08  601857.XSHG  10.85  10.86  11.69  10.74  478644195.0   \n",
       "4     2015-01-09  601857.XSHG  10.74  10.74  11.30  10.53  334132540.0   \n",
       "...          ...          ...    ...    ...    ...    ...          ...   \n",
       "24395 2015-12-25  601186.XSHG  12.54  12.64  12.67  12.41   20541178.0   \n",
       "24396 2015-12-28  601186.XSHG  12.71  12.23  12.71  12.20   36161860.0   \n",
       "24397 2015-12-29  601186.XSHG  12.25  12.28  12.31  12.18   20947996.0   \n",
       "24398 2015-12-30  601186.XSHG  12.36  12.34  12.50  12.27   21991434.0   \n",
       "24399 2015-12-31  601186.XSHG  12.34  12.27  12.53  12.25   28287535.0   \n",
       "\n",
       "              money  \n",
       "0      4.228175e+09  \n",
       "1      4.626943e+09  \n",
       "2      3.900464e+09  \n",
       "3      5.362077e+09  \n",
       "4      3.640000e+09  \n",
       "...             ...  \n",
       "24395  2.579197e+08  \n",
       "24396  4.518754e+08  \n",
       "24397  2.561569e+08  \n",
       "24398  2.717333e+08  \n",
       "24399  3.501418e+08  \n",
       "\n",
       "[24400 rows x 8 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取多只股票\n",
    "panel =  get_price(get_index_stocks('000903.XSHG')) # 获取中证100的所有成分股的2015年的天数据, 返回一个[pandas.Panel]\n",
    "panel"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 获取财务数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`get_fundamentals(query_object, date=None, statDate=None)`\n",
    "\n",
    "查询财务数据，详细的数据字段描述请点击[财务数据文档](https://www.joinquant.com/help/api/help?name=Stock#财务数据列表)查看\n",
    "\n",
    "date和statDate参数只能传入一个:\n",
    "\n",
    "- 传入date时, 查询**指定日期date收盘后所能看到的最近(对市值表来说, 最近一天, 对其他表来说, 最近一个季度)的数据**, 我们会查找上市公司在这个日期之前(包括此日期)发布的数据, 不会有未来函数.\n",
    "- 传入statDate时, 查询 **statDate 指定的季度或者年份的财务数据**. 注意:\n",
    "\n",
    "1. 由于公司发布财报不及时, 一般是看不到当季度或年份的财务报表的, 回测中使用这个数据可能会有未来函数, 请注意规避.\n",
    "2. 由于估值表每天更新, 当按季度或者年份查询时, 返回季度或者年份最后一天的数据\n",
    "3. 由于“资产负债数据”这个表是存量性质的， 查询年度数据是返回第四季度的数据。\n",
    "4. 银行业、券商、保险专项数据只有年报数据，需传入statDate参数，当传入 date 参数 或 statDate 传入季度时返回空，请自行避免未来函数。\n",
    "\n",
    "当 date 和 statDate 都不传入时, 相当于使用 date 参数, date 的默认值下面会描述.\n",
    "\n",
    "**参数**\n",
    "\n",
    "- query_object: 一个sqlalchemy.orm.query.Query对象，可以通过全局的 query 函数获取 Query 对象，[query简易教程](https://www.joinquant.com/view/community/detail/16411)\n",
    "- date: 查询日期, 一个字符串(格式类似'2015-10-15')或者[datetime.date]/[datetime.datetime]对象, 为None时返回昨天（如果昨天非交易日，也会返回昨天的，不是上一个交易日）的数据；\n",
    "- statDate: 财报统计的季度或者年份, 一个字符串, 有两种格式:\n",
    "\n",
    "1. 季度: 格式是: **年 + 'q' + 季度序号**, 例如: '2015q1', '2013q4'.\n",
    "2. 年份: 格式就是年份的数字, 例如: '2015', '2016'.\n",
    "\n",
    "**返回** 返回一个 [pandas.DataFrame], 每一行对应数据库返回的每一行(可能是几个表的联合查询结果的一行), 列索引是你查询的所有字段 注意：\n",
    "\n",
    "1. **为了防止返回数据量过大, 我们每次最多返回10000行**\n",
    "2. 当相关股票上市前、退市后，财务数据返回各字段为空"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>code</th>\n",
       "      <th>pe_ratio</th>\n",
       "      <th>turnover_ratio</th>\n",
       "      <th>pb_ratio</th>\n",
       "      <th>ps_ratio</th>\n",
       "      <th>pcf_ratio</th>\n",
       "      <th>capitalization</th>\n",
       "      <th>market_cap</th>\n",
       "      <th>circulating_cap</th>\n",
       "      <th>circulating_market_cap</th>\n",
       "      <th>day</th>\n",
       "      <th>pe_ratio_lyr</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5024884</td>\n",
       "      <td>000001.XSHE</td>\n",
       "      <td>7.4984</td>\n",
       "      <td>0.4116</td>\n",
       "      <td>1.0593</td>\n",
       "      <td>1.8748</td>\n",
       "      <td>1.0246</td>\n",
       "      <td>1430867.625</td>\n",
       "      <td>1598.2791</td>\n",
       "      <td>1180405.5</td>\n",
       "      <td>1318.5129</td>\n",
       "      <td>2015-10-15</td>\n",
       "      <td>8.0713</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        id         code  pe_ratio  turnover_ratio  pb_ratio  ps_ratio  \\\n",
       "0  5024884  000001.XSHE    7.4984          0.4116    1.0593    1.8748   \n",
       "\n",
       "   pcf_ratio  capitalization  market_cap  circulating_cap  \\\n",
       "0     1.0246     1430867.625   1598.2791        1180405.5   \n",
       "\n",
       "   circulating_market_cap         day  pe_ratio_lyr  \n",
       "0               1318.5129  2015-10-15        8.0713  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "q = query(\n",
    "    valuation\n",
    ").filter(\n",
    "    valuation.code == '000001.XSHE'\n",
    ")\n",
    "df = get_fundamentals(q, '2015-10-15')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>statDate</th>\n",
       "      <th>code</th>\n",
       "      <th>basic_eps</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2014-12-31</td>\n",
       "      <td>000001.XSHE</td>\n",
       "      <td>1.73</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     statDate         code  basic_eps\n",
       "0  2014-12-31  000001.XSHE       1.73"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询平安银行2014年的年报\n",
    "q = query(\n",
    "        income.statDate,\n",
    "        income.code,\n",
    "        income.basic_eps,\n",
    "    ).filter(\n",
    "        income.code == '000001.XSHE',\n",
    "    )\n",
    "\n",
    "ret = get_fundamentals(q, statDate='2014')\n",
    "ret"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 股票收益率"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "股票周期简单收益率： $ R_t=\\frac{P_t-P_{t-1}}{P_{t-1}} $ \\\n",
    "对数形式：   $ r_t=\\ln(1+R_t)=\\ln(\\frac{P_t}{P_{t-1}}) $"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>close</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-09-02</th>\n",
       "      <td>0.011889</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-09-03</th>\n",
       "      <td>-0.027415</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-09-04</th>\n",
       "      <td>0.004027</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-09-07</th>\n",
       "      <td>-0.001337</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-09-08</th>\n",
       "      <td>0.032798</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-09-24</th>\n",
       "      <td>-0.032630</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-09-25</th>\n",
       "      <td>0.004630</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-09-28</th>\n",
       "      <td>0.007900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-09-29</th>\n",
       "      <td>-0.033312</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-09-30</th>\n",
       "      <td>0.025000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>21 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "               close\n",
       "2020-09-02  0.011889\n",
       "2020-09-03 -0.027415\n",
       "2020-09-04  0.004027\n",
       "2020-09-07 -0.001337\n",
       "2020-09-08  0.032798\n",
       "...              ...\n",
       "2020-09-24 -0.032630\n",
       "2020-09-25  0.004630\n",
       "2020-09-28  0.007900\n",
       "2020-09-29 -0.033312\n",
       "2020-09-30  0.025000\n",
       "\n",
       "[21 rows x 1 columns]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = get_price('000001.XSHE', start_date='2020-09-01', end_date='2020-09-30 23:00:00',  \\\n",
    "               frequency='daily', fields=['close'])\n",
    "Rt =df.pct_change().dropna()\n",
    "Rt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
